#include "PauseState.h"
#include "Application.h"
#include "Fonts.h"
#include "Utils.h"
#include "Params.h"
#include <sfml/Graphics.hpp>

PauseState::PauseState()
{
	m_cNormal = sf::Color::White;
	m_cSelected = sf::Color::Yellow;
	m_cShadow = sf::Color::Color(50,50,50);

	sf::Font* font = Fonts::Istance()->GetFont("Consola");
	m_menu[0] = sf::Text("Resume Game", (*font), 26);
	m_menu[1] = sf::Text("...", (*font), 26);
	m_menu[2] = sf::Text("...", (*font), 26);
	m_menu[3] = sf::Text("End Match", (*font), 26);

	m_shadows[0] = sf::Text("Resume Game", (*font), 26);
	m_shadows[1] = sf::Text("...", (*font), 26);
	m_shadows[2] = sf::Text("...", (*font), 26);
	m_shadows[3] = sf::Text("End Match", (*font), 26);

	float maxWidth = 0.f;
	float maxHeight = 0.f;

	for (int i  = 0; i < P_Elements ; i++)
	{
		maxWidth = (m_menu[i].GetRect().Width>maxWidth) ? m_menu[i].GetRect().Width : maxWidth;
		maxHeight = (m_menu[i].GetRect().Height>maxHeight) ? m_menu[i].GetRect().Height : maxHeight;
	}

	m_posMenu = sf::Vector2f((Application::Istance()->Window()->GetWidth()*0.5f ), 
								(Application::Istance()->Window()->GetHeight()*0.5f) - (maxHeight*0.5f*P_Elements));
	
	for (int i  = 0; i < P_Elements ; i++)
	{
		m_menu[i].SetPosition(sf::Vector2f(m_posMenu.x - m_menu[i].GetRect().Width*0.5f, 
											m_posMenu.y + maxHeight*i));
		m_shadows[i].SetPosition(sf::Vector2f(m_menu[i].GetPosition().x + 2.f, m_menu[i].GetPosition().y + 2.f));
		m_shadows[i].SetColor(m_cShadow);
	}

	m_selected = 0;
	m_startTimeSetted = false;
	m_gameState = NULL;
}

PauseState::~PauseState()
{
}

void PauseState::OnDraw(sf::RenderWindow& renderWindow)
{
	if (m_gameState != NULL)
		m_gameState->OnDraw(renderWindow);

	renderWindow.SaveGLStates();
	
	for (int i  = 0; i < P_Elements ; i++)
	{
		renderWindow.Draw(m_shadows[i]);
		m_menu[i].SetColor( ( i == m_selected) ? m_cSelected : m_cNormal );
		renderWindow.Draw(m_menu[i]);
	}

	renderWindow.RestoreGLStates();
}

void PauseState::OnIdle(const sf::Input& input)
{
	if(!m_startTimeSetted)
	{
		m_startTime = m_clock.GetElapsedTime();
		m_startTimeSetted = true;
		//
		m_gameState = static_cast<GameState*>(Application::Istance()->GetState("Game"));
		m_gameState->SetPaused();
	}

	if( Application::Istance()->IsInputAvailable() )
	{
		if (input.IsKeyDown(sf::Key::Escape))
		{
			OnExit();
			Application::Istance()->PauseInput();
			return;
		}
		else if (input.IsKeyDown(sf::Key::Return) || input.IsJoystickButtonDown(0,2) )
		{
			switch (m_selected)
			{
				case 0:
					{
						OnExit();
						Application::Istance()->PauseInput();
						return;
					}
					break;
				case 1:
					break;
				case 2:
					break;
				case 3:
					{
						m_startTimeSetted = false;
						Application::Istance()->SetState("Main");
						m_selected = 0;
					}
					break;
				default:
					break;
			}
			Application::Istance()->PauseInput(0.2f);
		}
		else if (input.IsKeyDown(sf::Key::Down) || input.GetJoystickAxis(0,sf::Joy::AxisY)>=50.f )
		{
			m_selected = (m_selected == P_Elements-1) ? 0 : m_selected + 1 ;
			Application::Istance()->PauseInput(0.2f);
		}
		else if (input.IsKeyDown(sf::Key::Up) || input.GetJoystickAxis(0,sf::Joy::AxisY)<=-50.f )
		{
			m_selected = (m_selected == 0) ? P_Elements-1 : m_selected - 1 ;
			Application::Istance()->PauseInput(0.2f);
		}
			
	}

}

void PauseState::OnExit()
{
	float pausedTime = m_clock.GetElapsedTime() - m_startTime;
	if (m_gameState->IsOn())
		m_gameState->AddPauseTime(pausedTime);
	m_gameState->SetPaused(false);
	m_startTimeSetted = false;
	Application::Istance()->SetState("Game");

	m_selected = 0;
}

